import { Component, OnInit } from '@angular/core';
import {DynamicsService, StInfo} from "../dynamics.service";
import {BodyOutputType, Toast, ToasterService} from "angular2-toaster";
import {SelectItem} from 'primeng/api';

declare function init(map): void;
declare function addPoint_st(data): void;
declare function switchTdtMap(type): void;


@Component({
  selector: 'distribute',
  templateUrl: './distribute.component.html',
  styleUrls: ['./distribute.component.scss']
})
export class DistributeComponent implements OnInit {

  maps: SelectItem[];

  selectedMap: string = 'vec';

  tools: any;

  selectedTool: any = {id: '1', name: 'max_2.png'};

  cities;

  stTypes;

  selectedCity = '1';

  selectedType = '';

  stInfo: StInfo[];

  cols: any[] = [
    { field: 'stcd', header: '测站编码' },
    { field: 'name', header: '测站名称' },
    { field: 'typeNm', header: '测站类型' }
  ];



  constructor(private dynamicsService: DynamicsService,
              private toasterService: ToasterService) {
    this.cities = [
      {name: '行政区划', id: ''},
      {name: '济南市', id: '1'}
    ];

    this.stTypes = [
      {name: '测站类型', id: ''},
      {name: '国家级监测站', id: '1'},
      {name: '省级监测站', id: '2'},
      {name: '普通监测站', id: '3'},
      {name: '统测站', id: '4'}
    ];

    this.maps = [
      {label: '地形图', value: 'vec'},
      {label: '交通图', value: 'ter'},
      {label: '影像图', value: 'img'}
    ];

    this.tools = [
      {id: '1', name: 'max_2.png'},
      {id: '2', name: 'min.png'},
      {id: '3', name: 'move.png'},
      {id: '4', name: 'select.png'},
      {id: '5', name: 'delete.png'},
    ];
  }

  ngOnInit() {
    init('mapDiv');
    this.search();
  }

  switchTdtMap(){
    switchTdtMap(this.selectedMap);
  }

  switchTool(){
    if(this.selectedTool.id=='1'){
      this.selectedTool.name = 'max_2.png';
      this.tools = [
        {id: '1', name: 'max_2.png'},
        {id: '2', name: 'min.png'},
        {id: '3', name: 'move.png'},
        {id: '4', name: 'select.png'},
        {id: '5', name: 'delete.png'},
      ];
    }
    if(this.selectedTool.id=='2'){
      this.selectedTool.name = 'min_2.png';
      this.tools = [
        {id: '1', name: 'max.png'},
        {id: '2', name: 'min_2.png'},
        {id: '3', name: 'move.png'},
        {id: '4', name: 'select.png'},
        {id: '5', name: 'delete.png'},
      ];
    }
    if(this.selectedTool.id=='3'){
      this.selectedTool.name = 'move_2.png';
      this.tools = [
        {id: '1', name: 'max.png'},
        {id: '2', name: 'min.png'},
        {id: '3', name: 'move_2.png'},
        {id: '4', name: 'select.png'},
        {id: '5', name: 'delete.png'},
      ];
    }
    if(this.selectedTool.id=='4'){
      this.selectedTool.name = 'select_2.png';
      this.tools = [
        {id: '1', name: 'max.png'},
        {id: '2', name: 'min.png'},
        {id: '3', name: 'move.png'},
        {id: '4', name: 'select_2.png'},
        {id: '5', name: 'delete.png'},
      ];
    }
    if(this.selectedTool.id=='5'){
      this.selectedTool.name = 'delete_2.png';
      this.tools = [
        {id: '1', name: 'max.png'},
        {id: '2', name: 'min.png'},
        {id: '3', name: 'move.png'},
        {id: '4', name: 'select.png'},
        {id: '5', name: 'delete_2.png'},
      ];
    }
  }

  search(){
    console.log(this.selectedType);
    console.log(this.selectedCity);
    this.queryStInfo();
  }

  queryStInfo(): void {
    this.dynamicsService.queryStInfo()
      .subscribe(
        (data: StInfo[]) => {addPoint_st(data),this.stInfo=data}, // success path
        error => this.errorTodo(error)
      )
  }

  errorTodo(error): void{
    const toast: Toast = {
      type: 'error',
      title: '请求失败',
      body: 'json格式解释错误',
      bodyOutputType: BodyOutputType.TrustedHtml,
    };
    this.toasterService.popAsync(toast);
  }

}
